/**
 * byte类型的处理工具类
 * @author: zhonglong.huang@hiscene.com
 * @create: 2020-12-08
 */
package com.my.number;

public class ByteUtil {

    /**
     * hex字符串转byte数组
     * @param hex
     * @return
     */
    public static byte[] hexToByteArray(String hex)
    {
        int l = hex.length();
        byte[] data = new byte[l / 2];
        for (int i = 0; i < l; i += 2)
        {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.length() == 0) {
            return null;
        }
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() >> 1];
        int index = 0;
        for (int i = 0; i < hexString.length(); i++) {
            if (index > hexString.length() - 1) {
                return byteArray;
            }
            byte highDit = (byte) (Character.digit(hexString.charAt(index), 16) & 0xFF);
            byte lowDit = (byte) (Character.digit(hexString.charAt(index + 1), 16) & 0xFF);
            byteArray[i] = (byte) (highDit << 4 | lowDit);
            index += 2;
        }
        return byteArray;
    }

    /**
     * 字节数组转16进制的字符串
     * @param bytes
     * @return
     */
    public static String bytesTohex(byte[] bytes, int offset, int len) {
        StringBuilder hex = new StringBuilder();
        for (int i = 0; i < len; i++) {
            byte b = bytes[offset ++];
            boolean flag = false;
            if (b < 0) {
                flag = true;
            }
            int absB = Math.abs(b);
            if (flag) {
                absB = absB | 0x80;
            }
            String tmp = Integer.toHexString(absB & 0xFF);
            /**
             * 转化的十六进制不足两位，需要补0
             */
            if (tmp.length() == 1) {
                hex.append("0");
            }
            hex.append(tmp.toLowerCase());
        }
        return hex.toString();
    }




    /**
     * int转byte数组，高位在前，低位在后
     * @param numb int数字
     * @return byte数组
     */
    public static byte[] intToByte(int numb) {
        byte[] b = new byte[4];
        b[3] = (byte) (numb & 0xff);
        b[2] = (byte) (numb >> 8 & 0xff);
        b[1] = (byte) (numb >> 16 & 0xff);
        b[0] = (byte) (numb >> 24 & 0xff);
        return b;
    }

    /**
     * 将两个byte数组合并
     * @param arr1 数组1 拼接后在前
     * @param arr2 数组2 拼接后在后
     * @return 拼接后的数组
     */
    public static byte[] concatByteArray(byte[] arr1, byte[] arr2){
        if(arr1 == null){
            return arr2;
        }

        if(arr2 == null){
            return arr1;
        }

        byte[] temp = new byte[arr1.length + arr2.length];
        System.arraycopy(arr1, 0, temp, 0, arr1.length);
        System.arraycopy(arr2, 0, temp, arr1.length, arr2.length);
        return temp;
    }

    public static byte[] getSubBytes(byte[] arr1, int start, int end){
        if(end >= start) {
            byte[] temp = new byte[end - start + 1];
            if (arr1 == null || arr1.length < end) {
                return null;
            }
            for (int i = start; i <= end; i++) {
                temp[i - start] = arr1[i];
            }
            return temp;
        }
        return null;
    }
}
